﻿using CloudDcPc.Bean;
using CloudDcPc.Entity;
using CloudDcPc.Util;
using log4net;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace CloudDcPc
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        ILog l;
        public App()
        {
            DispatcherUnhandledException += App_DispatcherUnhandledException;
            int day = DateTime.Today.Day;
            if (day%10 == 0)
            {
                try
                {
                    if (!File.Exists("LastMail_"+day))
                    {
                        SendMail mail = new SendMail();
                        ZipClass zip = new ZipClass();
                        zip.ZipFileFromDirectory("logs/" + DateTime.Now.ToString("yyyy-MM"), "GuDuLogs-" + DateTime.Now.ToString("yyyy-MM") + ".zip", 9);
                        mail.SendMailByPlainFormat("822846768@qq.com", DateTime.Now.ToLongTimeString(), "GuDuLogs-" + DateTime.Now.ToString("yyyy-MM") + ".zip", "GuDuLogs-" + DateTime.Now.ToString("yyyy-MM") + ".zip");
                        for (int i = 1; i <= 31;i++ )
                        {
                            if (File.Exists("LastMail_" + i))
                                File.Delete("LastMail_" + i);
                        }
                        File.Create("LastMail_" + day);
                    }
                }catch { }
            }
            log4net.Config.XmlConfigurator.Configure();
            l = log4net.LogManager.GetLogger("Login");
        }

        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
            stringBuilder.AppendFormat("应用程序出现了未捕获的异常，{0}/n", e.Exception.Message);
            if (e.Exception.InnerException != null)
            {
                stringBuilder.AppendFormat("/n {0}", e.Exception.InnerException.Message);
            }
            stringBuilder.AppendFormat("/n {0}", e.Exception.StackTrace);
            l.Error(stringBuilder.ToString());
            MessageBox.Show(stringBuilder.ToString());
            e.Handled = true;  
        }

        public static User user = new User();

        private static StatisticBean statisticBean = new StatisticBean();
        public static StatisticBean StatisticBean
        {
            get { return statisticBean; }
        }

        private static TogetherBean togetherBean = new TogetherBean();
        public static TogetherBean TogetherBean
        {
            get { return togetherBean; }
        }

        private static MyMenuBean myMenuBean = new MyMenuBean();
        public static MyMenuBean MyMenuBean
        {
            get { return myMenuBean; }
        }
        private static GzBean gzBean = new GzBean();
        public static GzBean GzBean
        {
            get { return gzBean; }
        }

        private static UserBean userBean = new UserBean();
        public static UserBean UserBean
        {
            get { return userBean; }
        }
        private static OrderBean orderBean = new OrderBean();
        public static OrderBean OrderBean
        {
            get { return orderBean; }
        }
        private static OrdersBean ordersBean = new OrdersBean();
        public static OrdersBean OrdersBean
        {
            get { return ordersBean; }
        }
        private static TabBean tabBean = new TabBean();
        public static TabBean TabBean
        {
            get { return tabBean; }
        }
        private static CheckOutBean checkOutBean = new CheckOutBean();
        public static CheckOutBean CheckOutBean
        {
            get { return checkOutBean; }
        }
        private static CheckOutGzBean checkOutGzBean = new CheckOutGzBean();
        public static CheckOutGzBean CheckOutGzBean
        {
            get { return checkOutGzBean; }
        }
        private static MenuTypeBean menuTypeBean = new MenuTypeBean();
        public static MenuTypeBean MenuTypeBean
        {
            get { return menuTypeBean; }
        }
    }
}
